package com.cch.cooperation.biz.common.util;

import cn.hutool.core.util.ObjectUtil;
import com.cch.cooperation.common.exception.BizException;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

public class BeanUtilEx {

    public static <T> T copy(Object obj, Class<T> c) {
        if (ObjectUtil.isNull(obj)) {
            return null;
        }
        try {
            T result = c.newInstance();
            BeanUtils.copyProperties(obj, result);
            return result;
        } catch (Exception e) {
            throw new BizException("对象 copy 失败", e);
        }
    }

    public static <T, R> List<R> copy(List<T> list, Class<R> c) {
        List<R> result = new ArrayList<>();
        for (T item : list) {
            result.add(copy(item, c));
        }
        return result;
    }

}
